package com.snake.springboot.custom.service.assemble.goods;


import com.snake.springboot.custom.api.model.bill.BillEnum;
import com.snake.springboot.custom.api.model.bill.BillPlaceOrderModel;
import com.snake.springboot.custom.api.model.goods.CustStryGoodsDetailModel;
import com.snake.springboot.custom.api.request.bill.BillStrgGoodsPlaceDetailModel;
import com.snake.springboot.custom.api.request.bill.BillStrgGoodsPlaceModel;
import com.snake.springboot.custom.api.request.goods.CustStryGoodsQuery;
import com.snake.springboot.custom.api.response.goods.StrategyGoodsRsp;
import com.snake.springboot.custom.dao.mybatis.entity.goods.CustStryGoodsDO;
import org.snake.common.code.SerialNumberGeneratorFactory;
import org.snake.common.utils.ListUtils;
import org.snake.common.utils.PropertyUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>商户购买策略商品组装器
 * 类{@code CustStryGoodsAssemble} </br>
 * 创建时间2025/3/27 18:57
 *
 * @author yangxt
 * @see
 */
public class CustStryGoodsAssemble {
    

    public static CustStryGoodsDO toGoodsDO(CustStryGoodsQuery query) {
        CustStryGoodsDO goodsDO = new CustStryGoodsDO();
        PropertyUtils.copyProperties(goodsDO,query);
        return goodsDO;
    }




    public static List<CustStryGoodsDetailModel> toDetailList(BillStrgGoodsPlaceModel model,
                                                              List<StrategyGoodsRsp> details,
                                                              BillPlaceOrderModel placeOrderModel,
                                                              List<CustStryGoodsDO> goodsDOS) {

        placeOrderModel.setBillId(SerialNumberGeneratorFactory.buildId4Bean());
        placeOrderModel.setPayAmount(BigDecimal.ZERO);
        placeOrderModel.setBillType(BillEnum.BillType.STRATEGY_PAY.getCode());
        placeOrderModel.setByAccount(model.getByAccount());
        placeOrderModel.setCustomId(model.getCustomId());
        placeOrderModel.setByAccount(model.getByAccount());

        List<CustStryGoodsDetailModel> models = new ArrayList<>(4);
        List<BillStrgGoodsPlaceDetailModel> detailList = model.getDetailList();
        for (BillStrgGoodsPlaceDetailModel detailModel : detailList) {
            StrategyGoodsRsp goodsRsp = details.stream().filter(x -> x.getStrategyGoodsId().equals(detailModel.getStrategyGoodsId())).findFirst().get();
            CustStryGoodsDO goodsDO =new CustStryGoodsDO();
            PropertyUtils.copyProperties(goodsDO,goodsRsp);
            goodsDO.setCustomId(model.getCustomId());
            goodsDO.setBillId(placeOrderModel.getBillId());
            goodsDO.setCustStryGoodsId(SerialNumberGeneratorFactory.buildId4Bean());
            goodsDO.setPlaceNum(detailModel.getPlaceNum());
            goodsDOS.add(goodsDO);
            //组装商品详情
            List<CustStryGoodsDetailModel> detailModels = ListUtils.copy(goodsRsp.getDetails(), CustStryGoodsDetailModel.class);
            for (CustStryGoodsDetailModel vo : detailModels) {
                vo.setStryGoodsDetailId(SerialNumberGeneratorFactory.buildId4Bean());
                vo.setCustStryGoodsId(goodsDO.getCustStryGoodsId());
            }
            models.addAll(detailModels);
            placeOrderModel.setPayAmount(placeOrderModel.getPayAmount().add(goodsDO.getSaleAmount().multiply(BigDecimal.valueOf(detailModel.getPlaceNum()))));
        }
        placeOrderModel.setGoodsName(details.stream().map(StrategyGoodsRsp::getGoodsName).collect(Collectors.joining(",")));
        return models;
    }
}
